const fs = require('fs')

exports.init = async function (browser) {
  let page = await browser.newPage();
  await page.setJavaScriptEnabled(true);
	await page.goto("https://juejin.cn/frontend");
  await page.waitForSelector('.entry-list-wrap .entry-list .item');
  return page;
}

exports.getList = async function (page) {
  let list = await page.$$eval('.entry-list-wrap .entry-list .title-row a', list => {
    return list.map(item => {
			return {
        title: item.innerText,
        href: item.href,
      }
		});
	});
  return list.slice(0, 10)
}

exports.getListDetail = async function (list, page) {
  for (let i = 0; i < list.length; i++) {
    await page.goto(list[i].href, {
      timeout: 0
    });
    list[i].content = await page.$eval('.article-content', e => e.innerHTML)
  }
  return list
}

exports.writeFile = async function (name, content) {
  fs.writeFileSync(name, content)
}
